import { readFileSync, existsSync } from 'fs'
import { defineEventHandler } from 'h3'

export default defineEventHandler((event) => {
  const id = event.context.params?.id
  if (!id) {
    throw createError({
      statusCode: 400,
      statusMessage: 'ID is required'
    })
  }

  const filePath = `shared-codes/${id}.json`
  if (!existsSync(filePath)) {
    throw createError({
      statusCode: 404,
      statusMessage: 'Shared code not found'
    })
  }

  const result = readFileSync(filePath, 'utf-8')
  return JSON.parse(result)
})
